package com.shopping.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shopping.commons.response.DataGradResp;
import com.shopping.commons.response.MessageResp;
import com.shopping.commons.util.CommonCode;
import com.shopping.mapper.AddressMapper;
import com.shopping.model.Address;
import com.shopping.param.AddressParam;
import com.shopping.service.IAddressService;

@Service    //这样写的话就会默认这个bean的名字是    addressService
public class AddressService implements IAddressService{
	
	@Autowired
	private AddressMapper addressMapper;
	
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public List<Address> findAddressByUser(Integer id){
		AddressParam param=new AddressParam();
		param.setUserid(id);
		param.setDelflag(CommonCode.DELFLAG_NORMAL);
		param.setStatus(CommonCode.STATUS_ONE);
		param.setRows(999);
		return addressMapper.select(param); 
	}

	public MessageResp insert(Address param) {
		MessageResp resp=new MessageResp();
		//TODO:   这里要对param进行一次加工，添加诸如  addrid    updateperson updatetime  delflag 
		//status     这样的一些参数
		Random r=new Random();
		param.setAddrid(r.nextInt(10000)+100000);//   100000-199999  之间的随机数
		param.setDelflag(CommonCode.DELFLAG_NORMAL);
		param.setCreatedate(sdf.format(new Date()));
		param.setStatus(CommonCode.STATUS_ONE);
		if(addressMapper.insertSelective(param)>0){
			//成功了
			resp.setCode("0000");
			resp.setMsg("添加成功");
			resp.setObj(param);
		}else{
			resp.setCode("9999");
			resp.setMsg("添加失败");
		}
		return resp;
	}

	public MessageResp update(Address param) {
		MessageResp resp=new MessageResp();
		Address entity=addressMapper.selectByPrimaryKey(param.getAddrid());
		if(entity==null){
			//没找到
			resp.setCode("9999");
			resp.setMsg("未找到修改项");
			return resp;
		}
//		entity.setDelflag("1");
//		entity.setUpdatedate(sdf.format(new Date()));
		entity.setAddress(param.getAddress());
		entity.setDelflag(param.getDelflag()==null?CommonCode.DELFLAG_NORMAL:param.getDelflag());
		entity.setReceiver(param.getReceiver());
		entity.setStatus(param.getStatus()==null?CommonCode.STATUS_ONE:param.getStatus());
		entity.setTel(param.getTel());
		entity.setUserid(param.getUserid());
		
		//自动更新   更新的时间  和更新的人
		entity.setUpdatedate(sdf.format(new Date()));
//		if(addressMapper.updateByPrimaryKey(param)>0){  hH会更新掉所有的字段，没有值的就用null覆盖
		if(addressMapper.updateByPrimaryKeySelective(entity)>0){
			//成功了
			resp.setCode("0000");
			resp.setMsg("修改成功");
		}else{
			resp.setCode("9999");
			resp.setMsg("修改失败");
		}
		return resp;
	}

	public MessageResp delete(Integer id) {
		//  在实现类中写的任何   与 接口无关的方法，都是在Controller中 不可见的
		MessageResp resp=new MessageResp();
		//TODO:如果没有这个id怎么办
		Address entity=addressMapper.selectByPrimaryKey(id);
		if(entity==null){
			//没找到
			resp.setCode("9999");
			resp.setMsg("未找到删除项");
			return resp;
		}
		entity.setDelflag(CommonCode.DELFLAG_DELETE);
		entity.setUpdatedate(sdf.format(new Date()));//asf.pares("2015-10-10 10:10:10")
//		try {
//			Integer.parseInt("1asdfa");
//		} catch (NumberFormatException e) {
//			e.printStackTrace();
//		}
		if(addressMapper.updateByPrimaryKeySelective(entity)>0){
			//成功了
			resp.setCode("0000");
			resp.setMsg("删除成功");
		}else{
			resp.setCode("9999");
			resp.setMsg("删除失败");
		}
		return resp;
	}

	public DataGradResp<Address> select(AddressParam param) {
		DataGradResp<Address> resp=new DataGradResp<Address>();
		//查询     可能有多个，可能是单个
		//如果前端没有传任何参数的话，delflag也要特殊处理下
		param.setDelflag(param.getDelflag()==null?"0":param.getDelflag());
		List<Address> addressList;
		try {
			addressList = addressMapper.select(param);//这里要改成参数累了，
		} catch (Exception e) {
			e.printStackTrace();
			resp.setCode("9999");
			resp.setMsg("查询失败");
			return resp;
		}
		resp.setCode("0000");
		resp.setMsg("查询成功");
//		resp.setObj(addressList);
		resp.setRows(addressList);
		resp.setTotal(addressMapper.count(CommonCode.DELFLAG_NORMAL));
		return resp;
	}

	/**
	 * 这个是原始的CommonService里面实现下来的，现在暂时不要用了
	 */
	@Deprecated
	public DataGradResp<Address> select(Address param) {
		return null;
	}

	public MessageResp batchDelete(String ids) {
		MessageResp resp=new MessageResp();
		String[] idss=null;
		try {
			idss=ids.split(",");
		} catch (Exception e) {
			resp.setCode(CommonCode.FAIL);
			resp.setMsg("参数有误");
		}
		if(addressMapper.batchDelete(idss)>0){
			resp.setCode(CommonCode.SUCCESS);
			resp.setMsg("删除成功");
		}else{
			resp.setCode(CommonCode.FAIL);
			resp.setMsg("删除失败");
		}
		return resp;
	}

}
